﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MUD.Data;

namespace MUD.Lib.Objects.Users
{
    
    internal partial class User : MudObject, IDataObject
    {
        // This file will hold the data-access portion of the object.

        public void Save()
        {
            try
            {
                Dictionary<string, string> properties = new Dictionary<string, string>();
                properties.Add("@Name", ShortDesc);
                DataSet ds = Provider.ExecuteSproc("Player_Query", properties);
                if (ds.Tables[0].Rows.Count <= 0)
                {
                    // Insert
                    properties.Add("@Password", Password);
                    properties.Add("@Email", Email);
                    ds = Provider.ExecuteSproc("Player_Insert", properties);
                }
                else
                {
                    // Update
                    properties.Add("@Password", Password);
                    properties.Add("@Email", Email);
                    ds = Provider.ExecuteSproc("Player_Update", properties);
                }
            }
            catch (Exception ex)
            {
                MasterObj.Log(ex.Message);
            }
        }

        public bool Load()
        {
            try
            {
                Dictionary<string, string> properties = new Dictionary<string, string>();
                properties.Add("@Name", ShortDesc);
                DataSet ds = Provider.ExecuteSproc("Player_Query", properties);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DataRow playerData = ds.Tables[0].Rows[0];
                    Username = playerData.Field<string>("PlayerName");
                    Password = playerData.Field<string>("Password");
                    Email = playerData.Field<string>("Email");
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                MasterObj.Log(ex.Message);
                return false;
            }
        }

        public void Delete()
        {
            try
            {
                Dictionary<string, string> properties = new Dictionary<string, string>();
                properties.Add("@Name", ShortDesc);
                DataSet ds = Provider.ExecuteSproc("Player_Delete", properties);
            }
            catch (Exception ex)
            {
                MasterObj.Log(ex.Message);
            }
        }

        public bool Validate_Login()
        {
            try
            {
                Dictionary<string, string> properties = new Dictionary<string, string>();
                properties.Add("@Name", ShortDesc);
                properties.Add("@Password", Password);
                DataSet ds = Provider.ExecuteSproc("Validate_Login", properties);
                return ds.Tables[0].Rows[0].Field<int>(0) > 0;
            }
            catch (Exception ex)
            {
                MasterObj.Log(ex.Message);
                return false;
            }
        }
    }
}
